<?php
namespace NewSong\Controller;
use Common\Controller\NewSongController;
class TestController extends NewSongController {


    /**
     * post请求
     * @param $url
     * @param $postdata
     * @return mixed
     */
    private function post($url,$postdata,$headers){

        $ch = curl_init(); //用curl发送数据给api


        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        $response = curl_exec($ch);
        curl_close( $ch );
        $rest = json_decode($response,true);
        return $rest;
    }

    public function getinfo(){
        ignore_user_abort(); //php一直运行



        $url = "https://fkcgm.5iape.com/Cgmindex/new_problem.html";
        $uid = 1;
        for ($i=0;$i<1000;$i++){
            if ($i%40==0){
                sleep(1);
            }

            $header = array();
            $header[] = 'uid:'.$uid;
            $header[] = 'gameid:2';

            $data = array(
                'number'=>3,
                'uid'=>$uid,
                'gameid'=>1
            );
            $res = $this->post($url,$data,$header);

            if ($res['code']!='200'){
                $uid++;
            }

                $data = S('data_res');

                $data[] = $res;

                S('data_res',$data);


        }

        exit('ok');



    }

    public function test(){

        //S('data_res2',$data);

        $data = S('data_res');

        foreach ($data as $key=>$val){

            if ($data[$key]['code'] == '400'){
                unset($data[$key]);
            }

        }

        S('newdata',$data);
        print_r($data);


    }

    public function quchong(){

        $date = S('newdata');

        $arr = array();

        foreach ($date as $key=>$value){

            $da = $date[$key]['data'];

            $question = $da['question'];
            foreach ($question as $k1=>$v1){

                $arr[$question[$k1]['id']] = $question[$k1];

            }

        }
        var_dump(count($arr));
        S('quchong',$arr);
        print_r($arr);

    }


    public function insert(){

//        $data[] =  S('arr');
//        $data[] = S('arr1');
//
//        $arr = array();
//        foreach ($data as $key=>$val){
//
//            $a = $data[$key];
//            foreach ($a as $k1=>$v1){
//
//                $arr[$a[$k1]['id']] = $a[$k1];
//
//            }
//
//        }
    exit;
       //$data =  S('arr2');
       $data =  S('da3');
        foreach ($data as $key=>$val){

            $data[$key]['option'] = json_encode($data[$key]['option']);
            $data[$key]['aswer_id'] = $data[$key]['id'] ;
            $data[$key]['id'] = '';
            M('newsong_questions')->data($data[$key])->add();
        }


    }


    public function getfile(){

        $data =  S('arr2');

        foreach ($data as $key=>$val){

            $audio = $val['audio'];

            $jiequ = substr($audio,strrpos($audio,'/')+1);

            $wjj = substr($audio,strrpos($audio,'/')-5);

            $a = substr($wjj,0,5);

            $user_path = 'Public/newsong/'.$a;

            $this->getImage($audio,$user_path,$jiequ);

            $data[$key]['audio'] = 'newsong/'.$a.'/'.$jiequ;


            $song = $val['song'];

            $jiequ = substr($song,strrpos($song,'/')+1);

            $wjj = substr($song,strrpos($song,'/')-5);

            $a = substr($wjj,0,5);

            $user_path = 'Public/newsong/'.$a;

            $this->getImage($song,$user_path,$jiequ);

            $data[$key]['song'] = 'newsong/'.$a.'/'.$jiequ;


            $picture = $val['picture'];

            $jiequ = substr($picture,strrpos($picture,'/')+1);

            $wjj = substr($picture,strrpos($picture,'/')-5);

            $a = substr($wjj,0,5);

            $user_path = 'Public/newsong/'.$a;

            $this->getImage($picture,$user_path,$jiequ);

            $data[$key]['picture'] = 'newsong/'.$a.'/'.$jiequ;

            if ($key%50==0){
                sleep(1);
            }

        }

        S('da3',$data);
        exit('ok');

    }

     public function jiequ(){

         $song = "http://osscgm.5iape.com/upload/share/fkcgm/MP3V0/b053.mp3";
         $jiequ = substr($song,strrpos($song,'/')-5);

         $a = substr($jiequ,0,5);



         var_dump($a);

     }

    public function arr(){

        $data =  S('da3');

        var_dump($data);
    }

    /**
     * 获取网络文件
     * @param $url
     * @param string $save_dir
     * @param string $filename
     * @param int $type
     * @return array
     */
    private function getImage($url,$save_dir='',$filename='',$type=0){
        if(trim($url)==''){
            return array('file_name'=>'','save_path'=>'','error'=>1);
        }
        if(trim($save_dir)==''){
            $save_dir='./';
        }
//        if(trim($filename)==''){//保存文件名
//            $ext=strrchr($url,'.');
//            if($ext!='.gif'&&$ext!='.jpg'){
//                return array('file_name'=>'','save_path'=>'','error'=>3);
//            }
//            $filename=time().$ext;
//        }
        if(0!==strrpos($save_dir,'/')){
            $save_dir.='/';
        }
        //创建保存目录
        if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
            return array('file_name'=>'','save_path'=>'','error'=>5);
        }
        //获取远程文件所采用的方法
        if($type){
            $ch=curl_init();
            $timeout=5;
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $img=curl_exec($ch);
            curl_close($ch);
        }else{
            ob_start();
            readfile($url);
            $img=ob_get_contents();
            ob_end_clean();
        }
        //$size=strlen($img);
        //文件大小
        $fp2=@fopen($save_dir.$filename,'a');
        fwrite($fp2,$img);
        fclose($fp2);
        unset($img,$url);
        return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
    }

    public function question(){

        exit;
        $ids = array(1,2,3,4,5);

        echo $ids[array_rand($ids)];
       // echo  rand(1,100000);

       $data = M('song_quersion1')->select();

       foreach ($data as $key=>$val){

           srand((float) microtime() * 10000000);
           $number = $ids[array_rand($ids)];
           $a =rand(1,100000)*10+1;
           $b = rand(1,100000)*10+2;
           $c = rand(1,100000)*10+3;

           $arr1 = array(
               'title'=>$val['puzzle1'],
               'id'=>$a,
           );

           $arr2 = array(
               'title'=>$val['puzzle2'],
               'id'=>$b,
           );

           $arr3 = array(
               'title'=>$val['puzzle3'],
               'id'=>$c,
           );
           $aswer_id = $a;
           $question[0] = $arr1;
           $question[1] = $arr2;
           $question[2] = $arr3;

           $suiji = $this->shuffle_assoc($question);
           $suiji = $this->shuffle_assoc($suiji);
           $suiji = array_merge($suiji);

           $option = array(
               'A'=>$suiji[0],
               'B'=>$suiji[1],
               'C'=>$suiji[2]
           );


           $title = $val['answer'];

           $singer = $val['singer'];

           $music_time = 10;

           if (empty($data[$key]['album'])){
               $song = 'music2/'.$data[$key]['songa'];
               $audio = 'music2/'.$data[$key]['songb'];
           }else{
               $song = 'song/'.$data[$key]['album'].'/'.$data[$key]['songa'];
               $audio = 'song/'.$data[$key]['album'].'/'.$data[$key]['songb'];
           }


           $song2 = $audio;


           if ($number=='3'||'4'||'5'){

               $song2 = $song;

           }

           $insert = array(
               'audio'=>$audio,
               'title'=>$title,
               'song'=>$song2,
               'singer'=>$singer,
               'number'=>$number,
               'option'=>json_encode($option),
               'music_time'=>$music_time,
               'aswer_id'=>$aswer_id
           );

           M('newsong_questions')->data($insert)->add();

          // $arr[] = $insert;

       }

     //  print_r($arr);

    }

    public function getnewsong($puzzle,$puzzle2)
    {
        srand((float) microtime() * 10000000);
        $data = S('newsong_questions');

        if (empty($data)){

            $data = M('newsong_questions')->field('title')->select();

            S('newsong_questions',$data);
        }

        $title = $data[array_rand($data)]['title'];

        if ($title==$puzzle||$title==$puzzle2){
            $this->getnewsong($puzzle,$puzzle2);
        }
        return $title;

    }

    public function insert2(){

        exit();
        $ids = array(1,2,3,4,5);

        // echo  rand(1,100000);

        $data = M('song_quersion2')->select();

        foreach ($data as $key=>$val){

            srand((float) microtime() * 10000000);
            $number = $ids[array_rand($ids)];
            $a =rand(1,100000)*10+1;
            $b = rand(1,100000)*10+2;
            $c = rand(1,100000)*10+3;

            $arr1 = array(
                'title'=>$val['answer'],
                'id'=>$a,
            );
            $puzzle2 = $this->getnewsong($val['answer'],'');

            $arr2 = array(
                'title'=>$puzzle2,
                'id'=>$b,
            );
            $puzzle3 = $this->getnewsong($val['answer'],$puzzle2);
            $arr3 = array(
                'title'=>$puzzle3,
                'id'=>$c,
            );

            $aswer_id = $a;
            $question[0] = $arr1;
            $question[1] = $arr2;
            $question[2] = $arr3;

            $suiji = $this->shuffle_assoc($question);
            $suiji = $this->shuffle_assoc($suiji);
            $suiji = array_merge($suiji);

            $option = array(
                'A'=>$suiji[0],
                'B'=>$suiji[1],
                'C'=>$suiji[2]
            );


            $title = $val['answer'];

            $singer = $val['singer'];

            $music_time = 10;

            if (empty($data[$key]['album'])){
                $song = 'music2/'.$data[$key]['songa'];
                $audio = 'music2/'.$data[$key]['songb'];
            }else{
                $song = 'song/'.$data[$key]['album'].'/'.$data[$key]['songa'];
                $audio = 'song/'.$data[$key]['album'].'/'.$data[$key]['songb'];
            }


            $song2 = $audio;


            if ($number=='3'||$number=='4'||$number=='5'){

                $song2 = $song;

            }

            $insert = array(
                'audio'=>$audio,
                'title'=>$title,
                'song'=>$song2,
                'singer'=>$singer,
                'number'=>$number,
                'option'=>json_encode($option),
                'music_time'=>$music_time,
                'aswer_id'=>$aswer_id
            );

            M('newsong_questions2')->data($insert)->add();

            // $arr[] = $insert;

        }

        //  print_r($arr);

    }

    public function chackquestion(){

        exit();
        $where['id'] = array('lt',303);
        $data = M('newsong_questions1')->where($where)->select();

        foreach ($data as $key=>$val){

            $val['id'] = null;
            M('newsong_questions2')->data($val)->add();

        }

    }

  public function xiuzheng(){

        exit;
      $where['id'] = array('gt',823);
      $where['number'] = array('lt',3);
      $data = M('newsong_questions2')->where($where)->select();

      foreach ($data as $val){

          $update = array(
              'song'=>$val['audio']
          );

          M('newsong_questions2')->where(array('id'=>$val['id']))->save($update);
      }

      var_dump($data);

    }

    public function quzhong(){

        exit();
        $data = M('newsong_questions2')->group('title')->order('id asc')->select();


        foreach ($data as $key=>$val){
            $val['id'] = $key+1;
            M('newsong_questions')->add($val);
        }

        echo count($data);
    }

    public function checkquestion(){

        $data = M('newsong_questions')->select();
        $num = 0;
        foreach ($data as $val){

            $option = json_decode($val['option'],true);

            if (trim($option['A']['title'])==trim($option['B']['title'])||trim($option['A']['title'])==trim($option['C']['title'])||trim($option['B']['title'])==trim($option['C']['title'])){
                    $num++;
                    $arr[] = $option;
                    $ids[] = $val['id'];
            }

        }
        var_dump($ids);
        var_dump($arr);
        echo $num;

    }

    public function xiu(){

        exit;
        $data = M('newsong_questions')->where(array('id'=>'411'))->find();

        $option = json_decode($data['option'],true);

        $option['A']['title'] = '放生';

        M('newsong_questions')->where(array('id'=>'411'))->save(array('option'=>json_encode($option)));
        $data1 = M('newsong_questions')->where(array('id'=>'411'))->find();

        var_dump(json_decode($data1['option'],true));

    }

}
